/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.edu.cugb.xvideoplayer.activity;

import cn.edu.cugb.xvideoplayer.R;
import cn.edu.cugb.xvideoplayer.controller.MymediaController;
import cn.edu.cugb.xvideoplayer.view.VideoView;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;


public class MediaPlayerVideoProtrait extends Activity{

    private MymediaController mMediaController;
    private VideoView mVideoView;
    private LinearLayout mVideoWraper;
    private boolean isLandScape;
    DisplayMetrics localDisplayMetrics;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.videoplayer);
        localDisplayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
        mVideoWraper = (LinearLayout) findViewById(R.id.video_wrap);
        mVideoView = (VideoView) findViewById(R.id.surface_view);
        mMediaController = new MymediaController(this);
        mVideoView.setVideoPath("/sdcard/wonder.mp4");
        mVideoView.setMediaController(mMediaController);
        mVideoView.requestFocus();
    }
    
	private void setFullscreen(boolean on) {
		Window win = getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}
	
	public boolean isLandscape() {
	    return isLandScape;
	}
	
	public void land(boolean value) {
        if (value) {               
            LinearLayout.LayoutParams wraperParams = (LinearLayout.LayoutParams)this.mVideoWraper.getLayoutParams();
            wraperParams.width = localDisplayMetrics.heightPixels + 20;
            wraperParams.height = localDisplayMetrics.widthPixels;
            mVideoWraper.setLayoutParams(wraperParams);
            LinearLayout.LayoutParams wideoParams = (LinearLayout.LayoutParams)this.mVideoView.getLayoutParams();
            wideoParams.width = localDisplayMetrics.heightPixels + 20;
            wideoParams.height = localDisplayMetrics.widthPixels;
            mVideoWraper.requestLayout();
            mVideoView.requestFocus();
            mVideoView.requestLayout();
            
            if (android.os.Build.VERSION.SDK_INT >= 9) {
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            } else {
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }                
        } else {
            LinearLayout.LayoutParams wraperParams = (LinearLayout.LayoutParams)this.mVideoView.getLayoutParams();
            wraperParams.width = localDisplayMetrics.widthPixels;
            wraperParams.height = 440;
            mVideoWraper.setLayoutParams(wraperParams);
            LinearLayout.LayoutParams wideoParams = (LinearLayout.LayoutParams)this.mVideoView.getLayoutParams();
            wideoParams.width = localDisplayMetrics.widthPixels;
            wideoParams.height = 440;
            mVideoWraper.requestLayout();
            mVideoView.requestFocus();
            mVideoView.requestLayout();
            if (android.os.Build.VERSION.SDK_INT >= 9) {
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
            } else {
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }                   
        } 	    
	}
    
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
		    isLandScape = true;
			setFullscreen(true);
			
		} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
		    isLandScape = false;
		}
		super.onConfigurationChanged(newConfig);
	}
}
